package cn.fanqinghong.ymcc.service.impl;

import cn.fanqinghong.ymcc.domain.CourseMarket;
import cn.fanqinghong.ymcc.domain.CourseUserLearn;
import cn.fanqinghong.ymcc.dto.NotifyDto;
import cn.fanqinghong.ymcc.dto.OrderCourseDto;
import cn.fanqinghong.ymcc.mapper.CourseUserLearnMapper;
import cn.fanqinghong.ymcc.service.ICourseMarketService;
import cn.fanqinghong.ymcc.service.ICourseService;
import cn.fanqinghong.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author superman.fan
 * @since 2022-07-20
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private ICourseService courseService;

    @Autowired
    private ICourseMarketService courseMarketService;

    /**
     * 保存课程购买记录
     * 1. 如果一个订单有多个课程就要保存多条记录
     * @param notifyDto
     */
    @Override
    public void saveUserLearn(NotifyDto notifyDto) {
        String orderNo = notifyDto.getOrderNo();
        String extParams = notifyDto.getExtParams();
        OrderCourseDto orderCourseDto = JSONObject.parseObject(JSON.toJSON(extParams).toString(), OrderCourseDto.class);

        orderCourseDto.getCourseId().forEach(courseId ->{
            CourseMarket courseMarket = courseMarketService.selectById(courseId);
            //通过课程id和登陆人查询数据
            CourseUserLearn learnTem = selectCourseIdAndLoginId(courseId, orderCourseDto.getLoginId());
            Date now = new Date();
            if (learnTem == null){//表示没有保存过课程记录，新增
                CourseUserLearn learn = new CourseUserLearn();
                learn.setCourseId(courseId);
                learn.setCourseOrderNo(orderNo);
                learn.setStartTime(now);
                learn.setCreateTime(now);
                Date end = DateUtils.addDays(now, courseMarket.getValidDays());
                learn.setEndTime(end);
                learn.setLoginId(orderCourseDto.getLoginId());
                learn.setState(CourseUserLearn.STATE_PAYED);
                insert(learn);
            }else {//续约，修改 覆盖订单号
                learnTem.setCourseOrderNo(orderNo);
                //如果过期，重置当前数据
                if (learnTem.getEndTime().before(now)){//过期了
                    learnTem.setLoginId(orderCourseDto.getLoginId());
                    Date endTime = DateUtils.addDays(now, courseMarket.getValidDays());
                    learnTem.setEndTime(endTime);
                    learnTem.setStartTime(now);
                    learnTem.setCourseId(courseId);
                    learnTem.setState(CourseUserLearn.STATE_PAYED);//修改订单状态
                    learnTem.setCreateTime(now);
                }else {//没有过期
                    Date endTime = DateUtils.addDays(learnTem.getEndTime(), courseMarket.getValidDays());
                    learnTem.setEndTime(endTime);//修改结束日期
                }
                updateById(learnTem);
            }
        });

    }

    /**
     * 通过课程id和登陆人查询数据
     * @param courseId 课程id
     * @param loginId 购买人id
     * @return
     */
    private CourseUserLearn selectCourseIdAndLoginId(Long courseId,Long loginId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("login_id",loginId);
        return selectOne(wrapper);
    }
}
